Laravel / Advanced / Convert Html to pdf
Convert HTML to PDF
-
Installation
fonts
https://www.justinmind.com/blog/best-google-web-fonts-website/
https://freefontsdownload.net/free-arial-font-160187.htm1. install package
composer require barryvdh/laravel-dompdf 2. Set Up Dompdf Extension
config/app.php and place the DomPDF services in providers and aliases array.'providers' => [ Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ 'PDF' => Barryvdh\DomPDF\Facade::class, ] 3. creates a config file and the locus of the file is config/dompdf.php
php artisan vendor:publish -
Coding
1. in controller
1. store in storageuse PDF; .... .... $pdf_doc = PDF::loadView('/pdf/export_pdf', $p); Storage::put('invoice.pdf', $pdf_doc->output()); 1. /pdf/export_pdf is blade inside resources/views 2. direct download if we want to download pdf file
2. Storage::put() save the file in storage folder
3. direct open in browser$pdf_doc = PDF::loadView('/pdf/export_pdf', $p); return $pdf_doc->download('invoice1.pdf');
4. access file stored in storage$pdf_doc = PDF::loadView('/pdf/export_pdf', $p); return $pdf_doc->stream(); $headers = [ 'Content-Type' => 'application/pdf', ]; $orders=Orders::find($id); $path = storage_path().'/'.'app'.'/public/'.$orders->invoice_pdf; if (file_exists($path)) { return Response()->download($path,$orders->invoice_pdf,$headers); } -
Sample HTML
1. Multiple page
2. Header & footer
-
Custom font
1. google font
1.download font from https://fonts.google.com/
2. save the fonts in storage folder
2. Sample code
Kaushan Script
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
123456790
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
3. link the font
use bootstrap file
to check online https://www.w3schools.com/howto/tryit.asp?font=Kaushan%20Script ORdownload and put the fonts in storage/app/public/fonts
change the configuration font_dir to storage_path('app/public/fonts/')
change directory ownership to the webserver , chmod -R 777 storage
in view page call .ttf using asset()
@font-face { font-family: 'Arial Narrow Bold'; src: url('https://blog.manvia.in/storage/app/public/fonts/arial_narrow_normal_9c5a59f7d5311e9747faa4042ec12745.ttf') format("truetype"); font-style: bold; } body { font-family: 'Arial Narrow Bold', arial-narrow;font-size: 11px;font-weight:bold; line-height:12px; } -
Custom Size
$pdf = PDF::loadView('/pdf/purchase', $this->data)->setPaper('a5'); return $pdf->stream('purchase.pdf'); Kanadikavu -
Issues
Min height
SolutionHardwired Productions Dallas